﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace ConfigurationServer.AppCode
{
    public static class CEmail
    {
        public static void sendMail(List<string> lstAddresses,
                                      string strSubject,
                                      string strBody, Attachment attachment = null)
        {

            bool blnMainAdded = false;
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.EnableSsl = true; // configured by server specs.
            smtpClient.Timeout = 300000;

            MailMessage message = new MailMessage();


            try
            {
                foreach (string strEmail in lstAddresses)
                {
                    if (strEmail == "")
                        continue;

                    if (!blnMainAdded)
                        message.To.Add(strEmail);
                    else
                        message.Bcc.Add(new MailAddress(strEmail));
                }

                message.Subject = strSubject;
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;
                message.Body = strBody;
                if (attachment != null)
                    message.Attachments.Add(attachment);
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                throw new Exception("CEmail:sendMail:", ex);
            }
        }
    }
}